package cn.edu.scau.cmi.yangjiamin.springhibernate;

import org.hibernate.SessionFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.List;

public class TeacherDao {

	private static SessionFactory factory;
	private ArrayList<Teacher> teaList = new ArrayList<>();
	
	static {
		try {
			factory = new Configuration().configure("cn/edu/scau/cmi/yangjiamin/springhibernate/hibernate.cfg.xml")
					.buildSessionFactory();
		} catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		}
	}

	// 增
	public Integer addTea(String tea_name) {
		Session session = factory.openSession();
		Transaction tx = null;
		Integer tea_id = null;
		try {
			tx = session.beginTransaction();
			Teacher teacher = new Teacher(tea_name);
			tea_id = (Integer) session.save(teacher);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return tea_id;
	}

	// 删
	public void deleteTea(Integer tea_id) {
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Teacher teacher = (Teacher) session.get(Teacher.class, tea_id);
			if(teacher != null) {
			session.delete(teacher);
			}
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	// 改
	public void updateTea(Integer tea_id, String tea_name) {
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Teacher teacher = (Teacher) session.get(Teacher.class, tea_id);
			teacher.setTea_name(tea_name);
			session.update(teacher);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	// 添加学生
	public void addStu(String tea_id, String stus_id) {
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Teacher teacher = (Teacher) session.get(Teacher.class, Integer.parseInt(tea_id));
			if (teacher == null) {
				System.out.println("没有" + tea_id + "工号的老师");
				tx.commit();
			} else {
				Set<Student> test = teacher.getStudents();
				if (test.size() != 0) {
					System.out.println("该老师已有指导的学生，请选择修改指导的学生");
				} else {
					String[] stu_id = stus_id.split(" ");
					Set<Student> students = new HashSet<Student>();
					for (int i = 0; i < stu_id.length; i++) {
						Student student = (Student) session.get(Student.class, Integer.parseInt(stu_id[i]));
						if (student == null) {
							System.out.println("没有这个学生" + stu_id[i]);
						} else {
							students.add(student);
							student.setTea(teacher);
							session.update(student);
						}
					}
					for (Iterator iterator = students.iterator(); iterator.hasNext();) {
						Student addStu = (Student) iterator.next();
					}
					teacher.setStudents(students);
					session.update(teacher);
				}
				tx.commit();
			}
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void updateStu(String tea_id, String stus_id) {
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Teacher teacher = (Teacher) session.get(Teacher.class, Integer.parseInt(tea_id));
			if (teacher == null) {
				System.out.println("没有这个老师" + tea_id);
				tx.commit();
			} else {
				Set<Student> test = teacher.getStudents();
				if (test.size() == 0) {
					System.out.println("该老师没有指导的学生，请选择添加指导学生");
				} else {
					String[] stu_id = stus_id.split(" ");
					Set<Student> students = new HashSet<Student>();
					for (int i = 0; i < stu_id.length; i++) {
						Student student = (Student) session.get(Student.class, Integer.parseInt(stu_id[i]));
						if (student == null) {
							System.out.println("没有这个学生" + stu_id[i]);
						} else {
							students.add(student);
							student.setTea(teacher);
							session.update(student);
						}
					}
					for (Iterator iterator = students.iterator(); iterator.hasNext();) {
						Student addStu = (Student) iterator.next();
						System.out.print("Name: " + addStu.getStu_id());
					}
					teacher.setStudents(students);
				}
				tx.commit();
			}
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("rawtypes")
	public ArrayList getTeaList() {
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			teaList = new ArrayList<>();
			teaList = (ArrayList) session.createQuery("FROM Teacher").list();
			System.out.println("getTeachers");
			for (Iterator iterator = ((java.util.List) teaList).iterator(); iterator.hasNext();) {
				Teacher teacher = (Teacher) iterator.next();
				System.out.print("Name: " + teacher.getTea_name());
			}
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return teaList;
	}

}
